package view;

import model.Constants;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import view.listener.CloseButtonListener;
import view.listener.StartButtonListener;
import control.Util;

public class ConfigFrame {

	private Display display;
	private Shell shell;
	private Label playerName, enemyName;
	private Text playerNameField, enemyNameField;
	private Button start, close;

	public ConfigFrame() {

		display = new Display();
		shell = new Shell(display);
		configureShell();
		keepAlive();
	}

	private void buildContent() {

		playerName = new Label(shell, SWT.NULL);
		playerName.setText(Constants.PLAYERNAMELABEL);

		playerNameField = new Text(shell, SWT.BORDER);
		playerNameField.setLayoutData(new GridData(100, 15));

		enemyName = new Label(shell, SWT.NULL);
		enemyName.setText(Constants.ENEMYNAMELABEL);

		enemyNameField = new Text(shell, SWT.BORDER);
		enemyNameField.setLayoutData(new GridData(100, 15));

		start = new Button(shell, SWT.PUSH);
		start.setText(Constants.STARTBUTTONLABEL);
		start.addSelectionListener(new StartButtonListener());

		close = new Button(shell, SWT.PUSH);
		close.setText(Constants.CLOSEBUTTONLABEL);
		close.addSelectionListener(new CloseButtonListener());

	}

	private void configureShell() {
		shell.setLayout(getShellLayout());
		shell.setText(Constants.SHELLTITLE);
		buildContent();
		shell.pack();
		Util.centerFrame(shell);
		shell.setVisible(true);
	}

	private Layout getShellLayout() {
		GridLayout layout = new GridLayout(2, false);
		layout.marginLeft = 10;
		layout.marginRight = 10;
		layout.marginTop = 10;
		layout.marginBottom = 10;
		return layout;
	}

	private void keepAlive() {

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

	}
}
